package org.tomcat.unit502;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

public class SimpleContextMapper implements Mapper {

    private SimpleContext context = null;

    private String protocol = null;

    @Override
    public Container getContainer() {
        return context;
    }

    @Override
    public void setContainer(Container container) {
        this.context = (SimpleContext)container;
    }

    @Override
    public String getProtocol() {
        return this.protocol;
    }

    @Override
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    @Override
    public Container map(Request request, boolean update) {
        String contextPath = ((HttpServletRequest)request.getRequest()).getContextPath();
        String requestURI = ((HttpRequest)request).getRequestURI();
        String relativeURI = requestURI.substring(contextPath.length());

        Wrapper wrapper = null;
        String servletPath = relativeURI;
        String pathInfo = null;
        String name = context.findServletMapping(relativeURI);
        if (name != null) {
            wrapper = (Wrapper) context.findChild(name);
        }
        return wrapper;
    }
}
